<template>
  <div>
    <div>
      <el-form :inline="true" size="medium">
        <el-form-item label="出发地：">
          <el-input placeholder="全部" v-model="queryObj.start_address">
          </el-input>
        </el-form-item>
        <el-form-item clearable label="系列：">
          <el-select v-model="queryObj.series_id" placeholder="全部">
            <el-option
              v-for="item in seriesList"
              :key="item.series_id"
              :label="item.series_name"
              :value="item.series_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item clearable label="状态：">
          <el-select v-model="queryObj.audit_status" placeholder="全部">
            <el-option
              v-for="(item, key) in statusList"
              :key="key"
              :label="item"
              :value="key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="请输入线路名称/产品编号"
            v-model="queryObj.query_value"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getMainListReset"
            ><i class="iconfont icon-search"></i>查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="mb16">
        <el-button type="primary" size="medium" @click="goto('')"
          ><i class="iconfont icon-plus"></i>添加</el-button
        >
        <el-button type="danger" size="medium" @click="goto('')"
          ><i class="iconfont icon-delete"></i>删除</el-button
        >
      </div>
    </div>
    <div class="mytable">
      <el-table
        :data="mainList"
        tooltip-effect="dark"
        stripe
        style="width: 100%"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="产品编号" prop="commodity_code">
        </el-table-column>
        <el-table-column label="封面" width="200">
          <template slot-scope="scope">
            <img
              class="categoryimg"
              v-if="scope.row.commodity_img"
              :src="imgUrl + scope.row.commodity_img.split(',')[0]"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column label="系列" prop="series_name"> </el-table-column>
        <el-table-column
          label="线路名称"
          show-overflow-tooltip
          prop="commodity_name"
        >
        </el-table-column>
        <el-table-column width="100">
          <template slot="header">
            <span>价格（元）</span>
          </template>
          <template slot-scope="scope">
            {{ scope.row.pricing }}
          </template>
        </el-table-column>
        <el-table-column label="总库存" prop="repertory_count">
        </el-table-column>
        <el-table-column label="总销量" prop="commodity_count">
        </el-table-column>
        <el-table-column label="出发地" prop="start_address"> </el-table-column>
        <el-table-column width="120">
          <template slot="header">
            <span>成人利润（元）</span>
          </template>
          <template slot-scope="scope">
            {{ scope.row.cb_price_lr }}
          </template>
        </el-table-column>
        <el-table-column width="120">
          <template slot="header">
            <span>儿童利润（元）</span>
          </template>
          <template slot-scope="scope">
            {{ scope.row.child_cb_price_lr }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="150">
          <template slot-scope="scope">
            <!-- <el-switch
              v-model="scope.row.commodity_release"
              @change="statusChange($event, scope.row)"
              active-text="上架"
              inactive-text="下架"
              :active-value="2"
              :inactive-value="9">
            </el-switch> -->
            <span style="margin-right: 10px;">{{scope.row.commodity_release==2 ? '上架' : '下架'}}</span>
            <el-button type="text" key="1" @click="statusChange(2, scope.row)" v-if="scope.row.commodity_release==9 && scope.row.audit_status!=3">申请上架</el-button>
            <el-button type="text" key="2" @click="statusChange(9, scope.row)" v-if="scope.row.commodity_release==2 && scope.row.audit_status!=3">申请下架</el-button>
          </template>
        </el-table-column>
        <el-table-column label="审核状态">
          <template slot-scope="scope">
            <p>{{ statusList[scope.row.audit_status] }}</p>
            <el-button v-if="scope.row.audit_status != 3" type="text" @click="openAuditDlg(scope.row)">审核详情</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="role_name" label="排序">
          <template slot-scope="scope">
            <p>
              <i
                class="iconfont icon-arrowsupline bigicon"
                :class="scope.$index === 0 ? 'c999' : 'blue'"
                @click="rankUp(mainList, scope.$index)"
              ></i>
              <i
                class="iconfont icon-arrowsdownline bigicon"
                :class="scope.$index === mainList.length - 1 ? 'c999' : 'blue'"
                @click="rankDown(mainList, scope.$index)"
              ></i>
            </p>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="golook(scope.row)"
              ><i class="iconfont icon-liulan"></i>查看</el-button
            >
            <el-button type="text" @click="goto(scope.row)"
              ><i class="iconfont icon-edit"></i>编辑</el-button
            >
            <el-button type="text" @click="del(scope.row)"
              ><i class="iconfont icon-del"></i>删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagecon"
        background
        layout="total, sizes, prev, pager, next"
        @current-change="_mainList"
        @size-change="getMainListReset"
        :page-size.sync="page.limit"
        :page-sizes="pageSizeArr"
        :current-page.sync="page.page"
        :total="page.total"
      >
      </el-pagination>
    </div>

    <!-- // 审核详情对话框 -->
    <el-dialog title="审核详情" :visible.sync="dialogTableVisible" width="700">
      <el-table :data="auditList">
        <el-table-column label="审核时间" width="100">
          <template slot-scope="scope">
            <p>{{ scope.row.init_time | formatDate('yyyy-MM-dd hh:mm:ss') }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="user_name" label="审核人员"></el-table-column>
        <el-table-column label="状态变更为">
          <template slot-scope="scope">
            <p>{{ scope.row.ready_release == 9 ? '下架' : '上架'}}</p>
          </template>
        </el-table-column>
        <el-table-column  label="审核结果">
          <template slot-scope="scope">
            <p>{{ statusList[scope.row.audit_status] }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="反馈详情"></el-table-column>
      </el-table>
      <div slot="footer">
        <el-button type="primary" @click="dialogTableVisible = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
// import {
//   getSeriesPageList,
//   addOrModifySeries,
//   rankSeries,
//   deleteSeries
// } from 'api/lineManagement/seriesMgAPI';
import { getCommodityList, rankCommodity, getAuditList, soldOutCommodity, deleteCommodity } from "api/lineManagement/lineCommodity";
import { line_audit_status, line_status } from "common/config/constConfig";
import { pageSizeArr } from "common/config/constConfig";
import { getSeriesAll } from "api/lineManagement/seriesMgAPI";
import config from "api/config";

export default {
  components: {},
  computed: {},
  data() {
    return {
      statusList: line_audit_status,
      lineStatus: line_status,
      dialogMode: "add",
      showList: {
        1: "显示",
        0: "不显示"
      },
      auditList: [],
      imgUrl: config.imgUrl,
      dialogTableVisible: false,
      pageSizeArr,
      queryObj: {},
      mainList: [],
      seriesList: [],
      page: {
        total: 0,
        page: 1,
        limit: 10
      }
    };
  },
  methods: {
    openAuditDlg(row) {
      this.dialogTableVisible = true
      this.auditList = []
      // 获取详情
      this._getAuditList(row.commodity_id)
    },
    // 获取审核详情
    async _getAuditList(id) {
      let data = {
        commodity_id: id
      }
      let res = await getAuditList(data)
      this.auditList = res.rData
    },
    goto(row) {
      // ..
      if (row) {
        if(row.commodity_release == 2) {
          this.$confirm('编辑产品前要下架该产品，通过审核后方可进行编辑，是否申请下架商品？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.statusChange(9, row)
          })
        }else{
          this.$router.push({
            path: "/home/lineCommodityAdd",
            query: { id: row.commodity_id, type: "edit" }
          });
        }
      } else {
        this.$router.push("/home/lineCommodityAdd");
      }
    },
    golook(row) {
      this.$router.push({
        path: "/home/lineCommodityAdd",
        query: { id: row.commodity_id, type: "look" }
      });
    },
    // 申请上下架
    async statusChange(value, row) {
      await soldOutCommodity({
        commodity_id: row.commodity_id,
        ready_release: value
      })
      this._caozuoAfter('提交审核成功')
    },
    doAction() {
      let data = { ...this.form };
      data.series_img = this.imgs.join(",");
      // addOrModifySeries(data).then(() => {
      //   this._caozuoAfter('操作成功');
      // });
    },
    rankUp(arr, index) {
      if (index === 0) {
        return;
      }
      let sendObj = {};
      sendObj.curr_id = arr[index].commodity_id;
      sendObj.other_id = arr[index - 1].commodity_id;
      this._rank(sendObj);
    },
    rankDown(arr, index) {
      let len = arr.length;
      if (len - 1 === index) {
        return;
      }
      let sendObj = {};
      sendObj.curr_id = arr[index].commodity_id;
      sendObj.other_id = arr[index + 1].commodity_id;
      this._rank(sendObj);
    },
    _rank(data) {
      rankCommodity(data).then(res => {
        if (res) {
          this._caozuoAfter('操作成功');
        }
      });
    },
    del(row) {
      this.$confirm("是否确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this._del(row);
      });
    },
    _del(row) {
      deleteCommodity({commodity_id: row.commodity_id}).then(res => {
        this._caozuoAfter(res.rMsg);
      });
    },
    _caozuoAfter(msg) {
      this.dialogFormVisible = false;
      this.$message({
        message: msg,
        type: "success"
      });
      this._mainList();
    },
    getMainListReset() {
      this.page.page = 1;
      this._mainList();
    },
    changeMoney(arr) {
      arr.forEach(item => {
        item.cb_price_lr = (item.pricing - item.cb_price) / 100;
        item.child_cb_price_lr = (item.pricing - item.child_cb_price) / 100;
        item.pricing /= 100;
      });
      return arr;
    },
    // 获取
    _mainList() {
      let data = { ...this.queryObj };
      data.pageInfo = { ...this.page };
      getCommodityList(data).then(res => {
        let pageInfo = res.pageInfo;
        this.page.total = pageInfo.Total;
        this.page.page = pageInfo.page;
        this.mainList = this.changeMoney(res.rData);
      });
    },
    // huo qu 系列
    async _getSeriesAll() {
      let res = await getSeriesAll();
      this.seriesList = res.rData;
    }
  },
  created() {
    this._mainList();
    this._getSeriesAll();
  }
};
</script>

<style lang="scss" scoped></style>
